package com.kui.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kui.entity.User;
import com.kui.service.UserService;
import com.kui.util.HashUtil;

public class EditpwdServlet extends HttpServlet {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String name = user.getUsername();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		String old = request.getParameter("old");
		String newpwd = request.getParameter("newpwd");
		try {
			old = HashUtil.getHashString(old);
			System.out.println(old);
			newpwd = HashUtil.getHashString(newpwd);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UserService us = new UserService();
		User u = us.findByNameAndPassword(name, old);
		
		if(u != null){
			u.setPassword(newpwd);
			us.update(u);
			out.print("1");
		}else{
			out.print("0");
		}
		
		out.flush();
		out.close();
	}

}
